<template>
<div class="user">
    <div class="common-form">{{$t('plus.coupon_setting')}}</div>
    <div class="product-content">
      <el-form ref="form" :model="form" :rules="formRules" label-width="150px">
        <el-form-item :label="$t('plus.background_image')" prop="image" >
          <el-row>
            <el-button type="primary" @click="openUpload" size="mini">{{$t('plus.choose_image')}}</el-button>
            <div v-if="form.image != ''" class="img">
              <img :src="form.image" width="200" height="200" />
            </div>
          </el-row>
        </el-form-item>
      </el-form>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" @click="onSubmit" :loading="loading">{{$t('plus.submit')}}</el-button>
      </div>
    </div>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">{{$t('plus.upload_image_button')}}</Upload>
  </div>
</template>

<script>
import CardApi from '@/api/card.js';
import Upload from '@/components/file/Upload.vue';
import { formatModel } from '@/utils/base.js';
export default {
  components: {
    Upload
  },
  data() {
    return {
      form: {
        image: ''
      },
      /*是否打开图片选择*/
      isupload: false,
      formRules: {
        image: [{
          required: true,
          message: $t("plus.upload_bg"),
          trigger: 'blur'
        }]
      }
    };
  },
  created() {
    /*获取数据*/
    this.getData();
  },
  methods: {
    /*获取数据*/
    getData() {
      let self = this;
      CardApi.getSetting(true).then(res => {
        self.form = formatModel(self.form, res.data);
      }).catch(error => {});
    },
    /*提交表单*/
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          CardApi.editSetting(form, true).then(data => {
            self.loading = false;
            if (data.code == 1) {
              ElMessage({
                message: data.msg,
                type: 'success'
              });
            } else {
              self.loading = false;
            }
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      this.form.image = e[0].filePath;
      this.isupload = false;
    }
  }
};
</script>

